//
//  CaptureListViewController.m
//  CarPark
//
//  Created by oceans on 2019/8/8.
//  Copyright © 2019 oceans. All rights reserved.
//

#import "CaptureListViewController.h"
#import "CaptureListTableViewCell.h"
#import <MJRefresh/MJRefresh.h>
#import "CaptureDetailViewController.h"

@interface CaptureListViewController () <UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *array;
@property (strong, nonatomic) MetaModel *meta;
@end

@implementation CaptureListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray array];
    [self setupView];
    [self loadData];
}

- (void)loadData {
    
    defweakself
    [HttpRequest getURL:ktask_lists paras:@{@"type": @"task_capture_install"} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array addObjectsFromArray:result[@"datas"][@"tasks"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"tasks"][@"meta"][@"pagination"]];
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.tableView.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            weakself.meta = meta;
        }
        [weakself.tableView reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        
    }];
}

- (void)loadMoreData {
    
    defweakself
    [HttpRequest getURL:ktask_lists paras:@{@"type": @"task_capture_install", @"page": [NSString stringWithFormat:@"%d", _meta.current_page+1]} showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.array addObjectsFromArray:result[@"datas"][@"tasks"][@"data"]];
        MetaModel *meta = [[MetaModel alloc] initWithDic:result[@"datas"][@"tasks"][@"meta"][@"pagination"]];
        weakself.meta = meta;
        if (meta.current_page == meta.total_pages || meta.total == 0) {
            [weakself.tableView.mj_footer endRefreshingWithNoMoreData
             ];
        }else {
            [weakself.tableView.mj_footer endRefreshing];
        }
        [weakself.tableView reloadData];
    } failed:^(int code, NSString * _Nullable errMsg) {
        [weakself.tableView.mj_footer endRefreshing];
    }];
}

- (void)setupView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"捕捉设备安装任务";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, XNavigationHeight, ScreenWidth, ScreenHeight-XNavigationHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"CaptureListTableViewCell" bundle:nil] forCellReuseIdentifier:@"CaptureListTableViewCell"];
    _tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 135.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    /*"created_at" = "2019-06-30 02:10:14";
     dealer =                     {
     id = 1;
     name = "\U5927\U4f174S\U5e97";
     };
     "etc_time" = "2019-07-05 15:55:09";
     "fence_radius" = 1000;
     id = 1;
     parking =                     {
     "addr_full_name" = "\U5c71\U4e1c \U6d4e\U5357\U5e02 \U69d0\U836b\U533a";
     address = "\U7ecf\U5341\U8def11130\U53f7\U5927\U8f9b\U5e84";
     "contact_person" = "\U5929\U542f";
     "contact_tel" = 18556895623;
     id = 1;
     lat = "36.654064";
     lng = "116.966860";
     name = "\U5927\U8f9b\U5e84\U505c\U8f66\U573a";
     };
     status = 20;
     "status_text" = "\U5df2\U5b8c\U6210";*/
    CaptureListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CaptureListTableViewCell" forIndexPath:indexPath];
    NSDictionary *dic = _array[indexPath.row];
    cell.label1.text = [NSString stringWithFormat:@"%@%@", (dic[@"parking"][@"addr_full_name"] != [NSNull null] ? dic[@"parking"][@"addr_full_name"] : @""), dic[@"parking"][@"name"]];
    cell.label2.text = [NSString stringWithFormat:@"地址：%@", dic[@"parking"][@"address"]];
    cell.label3.text = [NSString stringWithFormat:@"任务时间：%@", dic[@"etc_time"]];
    cell.label4.text = [NSString stringWithFormat:@"%@", dic[@"status_text"]];
    if ([dic[@"status"] intValue] == 10) {
        cell.label5.text = @"立即安装";
    }else {
        cell.label5.text = @"查看";
    }
    if ([dic[@"status"] intValue] == 10) {
        cell.label4.textColor = COLOR(235, 205, 95, 1);
    }else if ([dic[@"status"] intValue] == 30) {
        cell.label4.textColor = COLOR(10, 235, 23, 1);
    }else if ([dic[@"status"] intValue] == 20) {
        cell.label4.textColor = COLOR(195, 195, 195, 1);
    }else {
        cell.label4.textColor = [UIColor blackColor];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CaptureDetailViewController *con = [[CaptureDetailViewController alloc] init];
    con._id = [_array[indexPath.row][@"id"] intValue];
    [self.navigationController pushViewController:con animated:YES];
}

@end
